package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.HousekeepingOrder;
import com.ruoyi.recycle.dto.HousekeepingOrderQueryDTO;
import com.ruoyi.recycle.service.IHousekeepingOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 家政订单Controller
 *
 * @author ruoyi
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/housekeepingOrder")
@Api(tags = "家政订单")
public class HousekeepingOrderController extends BaseController {
    @Autowired
    private IHousekeepingOrderService housekeepingOrderService;


    /**
     * 分页查询家政订单列表
     */
    @PostMapping("/housekeepingOrderPageQuery")
    @ApiOperation("分页查询家政订单列表")
    public IPage<HousekeepingOrder> housekeepingOrderPageQuery(@RequestBody HousekeepingOrderQueryDTO queryDTO) throws Exception {
        final HousekeepingOrder order = new HousekeepingOrder();
        BeanUtils.copyProperties(queryDTO, order);
        final LambdaQueryWrapper<HousekeepingOrder> wrapper = Wrappers.lambdaQuery(order);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(HousekeepingOrder::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeBegin()) + " 00:00:00"));
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(HousekeepingOrder::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeEnd()) + " 23:59:59"));
        }
        wrapper.orderByDesc(HousekeepingOrder::getCreatedTime);
        return housekeepingOrderService.page(new Page<>(queryDTO.getCurrentPage(), queryDTO.getPageSize(), true), wrapper);
    }

    /**
     * 导出家政订单列表
     */
    @RequiresPermissions("recycle:housekeepingOrder:export")
    @Log(title = "家政订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出家政订单列表")
    @ApiIgnore
    public void export(HttpServletResponse response, HousekeepingOrder housekeepingOrder) {
        List<HousekeepingOrder> list = housekeepingOrderService.selectHousekeepingOrderList(housekeepingOrder);
        ExcelUtil<HousekeepingOrder> util = new ExcelUtil<HousekeepingOrder>(HousekeepingOrder.class);
        util.exportExcel(response, list, "家政订单数据");
    }

    /**
     * 获取家政订单详细信息
     */
    @RequiresPermissions("recycle:housekeepingOrder:query")
    @GetMapping(value = "/{HKOrderNo}")
    @ApiOperation(value = "获取家政订单详细信息")
    @ApiImplicitParam(name = "HKOrderNo", value = "家政订单订单号", dataType = "String")
    public AjaxResult getInfo(@PathVariable("HKOrderNo") String HKOrderNo) {
        return AjaxResult.success(housekeepingOrderService.selectHousekeepingOrderById(HKOrderNo)).put("detail",housekeepingOrderService.selectHousekeepingOrderDetailById(HKOrderNo));
    }

    /**
     * 新增家政订单
     */
    @RequiresPermissions("recycle:housekeepingOrder:add")
    @Log(title = "家政订单", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增家政订单")
    public AjaxResult add(@RequestBody HousekeepingOrder housekeepingOrder) {
        return toAjax(housekeepingOrderService.insertHousekeepingOrder(housekeepingOrder));
    }

    /**
     * 修改家政订单
     */
    @RequiresPermissions("recycle:housekeepingOrder:edit")
    @Log(title = "家政订单", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改家政订单")
    public AjaxResult edit(@RequestBody HousekeepingOrder housekeepingOrder) {
        return toAjax(housekeepingOrderService.updateHousekeepingOrder(housekeepingOrder));
    }

    /**
     * 删除家政订单
     */
    @RequiresPermissions("recycle:housekeepingOrder:remove")
    @Log(title = "家政订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{HKOrderNos}")
    @ApiOperation("删除家政订单")
    @ApiImplicitParam(name = "HKOrderNos", value = "家政订单订单号集合", dataType = "List")
    public AjaxResult remove(@PathVariable String[] HKOrderNos) {
        return toAjax(housekeepingOrderService.deleteHousekeepingOrderByIds(HKOrderNos));
    }
}
